package com.example.chapter9;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
    public static Connection getCon() {
        try {
            String drivername = "com.mysql.cj.jdbc.Driver";
            String conUrl = "jdbc:mysql://localhost:3306/nieh?serverTimezone=GMT%2B8";//?useSSL=false
            String username = "root";
            String password = "Spl123Spl123";
            Connection con = null;

            Class.forName(drivername);
            try {
                con = DriverManager.getConnection(conUrl, username, password);
            } catch (SQLException e) {
                System.out.println("连接数据库的过程中出现SQL异常");
            }

            System.out.println("con");
            return con;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void release(ResultSet rs, Statement stmt, Connection con) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            rs = null;
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            stmt = null;
        }

        if (con != null) {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            con = null;
        }

    }

}